<?php
    class product_create_controller extends controllerBase
    {
        public function process()
        {
            database::isAdmin(true);
            if(isset($_POST['val']))
            {
                $aVals = $_POST['val'];
                if(isset($_FILES['image']['name']))
                {
                    if(!$_FILES['image']['error'])
                    {
                        $new_file_name = strtolower($_FILES['image']['name']);
                        $file_path = UPLOAD_PATH.$new_file_name;
                        if($_FILES['image']['size'] > (1024000))
                        {
                            $message = 'Oops!  Your file\'s size is to large.';
                            return $this->url->getLink('product/create' , true, $message);
                        }

                        if(move_uploaded_file($_FILES['image']['tmp_name'], $file_path))
                        {
                            $thumb = new easyphpthumbnail();
                            $thumb -> Thumbwidth = 300;
                            $thumb -> Thumbheight = 360;
                            $thumb -> Cropimage = array(3,0,0,0,0,0);
                            $thumb -> Thumblocation = UPLOAD_PATH.DIRECTORY_SEPARATOR.'300/';
                            $thumb -> Createthumb($file_path,'file');

                            $thumb = new easyphpthumbnail();
                            $thumb -> Thumbwidth = 500;
                            $thumb -> Thumbheight = 600;
                            $thumb -> Cropimage = array(3,0,0,0,0,0);
                            $thumb -> Thumblocation = UPLOAD_PATH.DIRECTORY_SEPARATOR.'500/';
                            $thumb -> Createthumb($file_path,'file');

                            $iImageId = $this->database->add('images', array(
                                'name' => $new_file_name,
                                'url' => FILE_URL.$new_file_name
                            ));
                            if($iImageId)
                            {
                                $aVals['image_id'] = $iImageId;
                            }
                            else
                            {
                                return $this->url->getLink('product/create', true, 'Upload file fail.');
                            }
                        }
                        else
                        {
                            return $this->url->getLink('product/create', true, 'Upload file fail.');
                        }
                    }
                    else
                    {
                        $message = 'Ooops!  Your upload triggered the following error:  '.$_FILES['photo']['error'];
                        return $this->url->getLink('product/create',true, $message);
                    }
                }
                else
                {
                    return $this->url->getLink('product/create', true, 'Upload file fail.');
                }
                
                $iProductId = $this->database->add('product',$aVals);
                if($iProductId)
                {
                    return $this->url->getLink('product/view/'.$iProductId, true, 'Add product successful.');
                }
                else
                {
                    return $this->url->getLink('product/create', true, 'Add product fail.');
                }
            }
            $aCateories = $this->database->getListObject('SELECT * FROM category');
            $this->set(array('aCategories' => $aCateories));
        }
    }
?>
